package cn.edu.xmu.oomall.aftersale.dao;

import cn.edu.xmu.oomall.aftersale.mapper.AfterSaleServicePoMapper;
import cn.edu.xmu.oomall.aftersale.model.bo.Aftersale;
import cn.edu.xmu.oomall.aftersale.model.po.AfterSaleServicePo;
import cn.edu.xmu.oomall.aftersale.model.po.AfterSaleServicePoExample;
import cn.edu.xmu.oomall.aftersale.model.vo.AftersaleRetVo;
import cn.edu.xmu.oomall.aftersale.model.vo.SimpleAftersaleVo;
import cn.edu.xmu.oomall.aftersale.service.AfterSaleService;
import cn.edu.xmu.oomall.core.model.VoObject;
import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.privilegegateway.annotation.util.RedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.lettuce.core.ScriptOutputType;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author X-Raymond
 * @date 2021/12/9
 */
@Repository
public class AfterSaleDao {
    private static final Logger logger = LoggerFactory.getLogger(AfterSaleDao.class);

    @Autowired
    AfterSaleServicePoMapper afterSaleServicePoMapper;

    /**
     * 根据查询条件获得分页的结果
     *
     * @param beginTime
     * @param state
     * @param endTime
     * @param page
     * @param pageSize
     * @return 售后服务列表 List<SimpleAftersaleVo>
     */
    public ReturnObject<PageInfo<VoObject>> getOwnAftersale(Long id, LocalDateTime beginTime, Integer state, LocalDateTime endTime, Integer page, Integer pageSize) {
        AfterSaleServicePoExample example = new AfterSaleServicePoExample();
        AfterSaleServicePoExample.Criteria criteria = example.createCriteria();
        criteria.andCustomerIdEqualTo(id);
        if (state != null) {
            criteria.andStateEqualTo(state.byteValue());
        }
        if (beginTime != null) {
            criteria.andGmtCreateGreaterThan(beginTime);
        }
        if (endTime != null) {
            criteria.andGmtCreateLessThan(endTime);
        }
        try {
            PageHelper.startPage(page, pageSize);
            List<AfterSaleServicePo> poList = afterSaleServicePoMapper.selectByExample(example);
            if (poList.isEmpty()) {
                return new ReturnObject<>(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            PageInfo<AfterSaleServicePo> pageInfo = new PageInfo<>(poList);
            ReturnObject returnObject = new ReturnObject(pageInfo);
            return Common.getPageRetVo(returnObject, SimpleAftersaleVo.class);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 查看售后详情
     *
     * @param id
     * @return
     */
    public ReturnObject getAftersalesById(Long id) {
        AfterSaleServicePo afterSaleServicePo = afterSaleServicePoMapper.selectByPrimaryKey(id);
        System.out.println(afterSaleServicePo);
        if (afterSaleServicePo == null) {
            return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
        }
        System.out.println("----1----");
        Aftersale aftersale = cloneVo(afterSaleServicePo, Aftersale.class);
        aftersale.setState(afterSaleServicePo.getState());
        return new ReturnObject(aftersale);
    }

    /**
     * 获取售后列表
     *
     * @param shopId
     * @param beginTime
     * @param endTime
     * @param state
     * @param type
     * @param page
     * @param pageSize
     * @return
     */
    public ReturnObject getAllAftersale(Long shopId, LocalDateTime beginTime, LocalDateTime endTime, Integer state, Integer type, Integer page, Integer pageSize) {
        AfterSaleServicePoExample example = new AfterSaleServicePoExample();
        AfterSaleServicePoExample.Criteria criteria = example.createCriteria();
        criteria.andShopIdEqualTo(shopId);
        if (state != null) {
            criteria.andStateEqualTo(state.byteValue());
        }
        if (beginTime != null) {
            criteria.andGmtCreateGreaterThan(beginTime);
        }
        if (endTime != null) {
            criteria.andGmtCreateLessThan(endTime);
        }
        if (type != null) {
            criteria.andTypeEqualTo(type.byteValue());
        }
        try {
            PageHelper.startPage(page, pageSize);
            List<AfterSaleServicePo> poList = afterSaleServicePoMapper.selectByExample(example);
            if (poList.isEmpty()) {
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            PageInfo<AfterSaleServicePo> pageInfo = new PageInfo<>(poList);
            ReturnObject returnObject = new ReturnObject(pageInfo);
            return Common.getPageRetVo(returnObject, SimpleAftersaleVo.class);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 根据id修改售后单
     *
     * @param id
     * @param aftersale
     * @return
     */
    public ReturnObject modifyAftersalesById(Long id, Aftersale aftersale) {
        AfterSaleServicePo afterSaleServicePo;
        int ret;
        try {
            afterSaleServicePo = (AfterSaleServicePo) cloneVo(aftersale, AfterSaleServicePo.class);
            afterSaleServicePo.setId(id);
            ret = afterSaleServicePoMapper.updateByPrimaryKeySelective(afterSaleServicePo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
        if (ret == 0) {
            return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
        } else {
            return new ReturnObject(ReturnNo.OK);
        }
    }

    /**
     * 管理员通过商铺id和id获得售后单
     *
     * @param shopId
     * @param id
     * @return
     */
    public ReturnObject getAftersalesByShopIdAndId(Long shopId, Long id) {
        try {
            AfterSaleServicePoExample afterSaleServicePoExample = new AfterSaleServicePoExample();
            AfterSaleServicePoExample.Criteria criteria = afterSaleServicePoExample.createCriteria();
            criteria.andIdEqualTo(id);
            criteria.andShopIdEqualTo(shopId);
            List<AfterSaleServicePo> afterSaleServicePos = afterSaleServicePoMapper.selectByExample(afterSaleServicePoExample);
            if (afterSaleServicePos.isEmpty()) {
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            AfterSaleServicePo afterSaleServicePo = afterSaleServicePos.get(0);
            Aftersale aftersale = (Aftersale) cloneVo(afterSaleServicePo, Aftersale.class);
            return new ReturnObject(aftersale);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    /**
     * 用户新增售后单
     *
     * @param aftersale
     * @return
     */
    public ReturnObject addAftersales(Aftersale aftersale) {
        AfterSaleServicePo afterSaleServicePo;
        int ret;
        try {
            afterSaleServicePo = (AfterSaleServicePo) cloneVo(aftersale, AfterSaleServicePo.class);
            ret = afterSaleServicePoMapper.insertSelective(afterSaleServicePo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
        if (ret == 0) {
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        } else {
            SimpleAftersaleVo simpleAftersaleVo;
            simpleAftersaleVo = (SimpleAftersaleVo) cloneVo(afterSaleServicePo, SimpleAftersaleVo.class);
            return new ReturnObject(simpleAftersaleVo);
        }
    }

    /**
     * 逻辑删除售后单
     *
     * @param id
     * @return
     */
    public ReturnObject logicDeleteAftersalesById(Long id) {
        int ret;
        try {
            ret = afterSaleServicePoMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
        if (ret == 0) {
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        } else {
            return new ReturnObject(ReturnNo.OK);
        }
    }

}
